package com.example.communitypro.controller;

import com.example.communitypro.pojo.*;
import com.example.communitypro.service.*;
import com.example.communitypro.util.DateUtil;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.*;

@Controller
@RequestMapping("/report")
public class UserReportController {

    @Autowired
    private UserReportService userReportService;
    @Autowired
    private PostService postService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserInformationService userInformationService;
    @Autowired
    private HomeMessageService homeMessageService;

    @ResponseBody
    @RequestMapping(value = "/addUserReport")
    @CrossOrigin
    public Result addUserReport(UserReport record){
        homeMessageService.add(record);
        return userReportService.addUserReport(record);
    }

    @ResponseBody
    @RequestMapping(value = "/dropUserReport")
    @CrossOrigin
    public Result dropUserReport(Integer reportNo){
        return userReportService.dropUserReport(reportNo);
    }

    @ResponseBody
    @RequestMapping(value = "/modifyUserReport")
    @CrossOrigin
    public Result modifyUserReport(UserReport record){
        return userReportService.modifyUserReport(record);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserReportBykey")
    @CrossOrigin
    public Result queryUserReport(Integer reportNo){
        return userReportService.queryUserReport(reportNo);
    }

    @ResponseBody
    @RequestMapping(value = "/queryUserInformationReport")
    @CrossOrigin
    public Result queryUserInformationReport(Integer user_no){
        return userReportService.queryUserInformationReport(user_no);
    }

    @ResponseBody
    @RequestMapping(value = "/queryPostReport")
    @CrossOrigin
    public Result queryPostReport(Integer postNo){
        return userReportService.queryPostReport(postNo);
    }

    @ResponseBody
    @RequestMapping(value = "/queryCommentReport")
    @CrossOrigin
    public Result queryCommentReport(Integer user_no){
        return userReportService.queryCommentReport(user_no);
    }


    /**
     * 初始化举报初始界面
     * @param
     * @return
     */
    @RequestMapping(value = "/getStartReport")
    @CrossOrigin
    @ResponseBody
    public Result getStartReport(){
        return userReportService.queryUserReport(-1,-1);
    }


    @ResponseBody
    @RequestMapping(value = "/queryUserReportByType")
    @CrossOrigin
    public Result queryUserReport(Integer no,Integer reportObjectType){
        return userReportService.queryUserReport(no,reportObjectType);
    }

    /**
     * 处理驳回
     * @param rejectText
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dealReject")
    @CrossOrigin
    public Map dealReject(String rejectText,String reportNo){
        Map<String,String> map = new HashMap<>();
        map.put("msg", rejectText);

        /**
         *后面应该完善的逻辑
         *驳回之后，举报该帖子或者评论或者用户
         * 的用户应该有信息提示
         */
        userReportService.dropUserReport(Integer.parseInt(reportNo));
        return map;


    }

    /**
     * 处理同意举报
     * @param no 举报对象编号
     * @param type 举报内容的类型
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dealAgree")
    @CrossOrigin
    public Result resReportForPost(String no,String type,String reportNo){
        if ("0".equals(type)){
            //处理举报类型为用户
            UserInformation userInformation = (UserInformation) userInformationService.queryUserInformation(Integer.parseInt(no)).getData().get("userInformation");
            userInformation.setUserStatus(0);
            userReportService.dropUserReport(Integer.parseInt(reportNo));
            return userInformationService.modifyUserInformation(userInformation);
        }else if ("1".equals(type)){
            //处理举报类型为帖子
            Post post = (Post) postService.queryPost(Integer.valueOf(no)).getData().get("post");
            userReportService.dropUserReport(Integer.parseInt(no));
            post.setPostStatus(0);
            userReportService.dropUserReport(Integer.parseInt(reportNo));
            return postService.modifyPost(post);
        }else {
            //处理举报类型为评论
            Comment comment = (Comment) commentService.queryComment(Integer.parseInt(no)).getData().get("comment");
            userReportService.dropUserReport(Integer.parseInt(no));
            comment.setCommentStatus(0);
            userReportService.dropUserReport(Integer.parseInt(reportNo));
            return  commentService.modifyComment(comment);
        }
//        postService.dropPost(Integer.valueOf(no));

    }


    @ResponseBody
    @RequestMapping(value = "/countReport")
    @CrossOrigin
    public List<Integer> countPost(){
        Date date = new Date();

        List<Integer> list = new ArrayList<>();
        list.add(userReportService.countByDate(DateUtil.getTimeByTransformation(date, "yyyy-MM-dd")));

        for (int i = 1; i <= 6; i++) {
            list.add(userReportService.countByDate(DateUtil.getTimeByTransformation(new Date(date.getTime()-24*3600*1000*i), "yyyy-MM-dd")));
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/queryAllReport")
    @CrossOrigin
    public Result queryALlReport(){
        /*Result result = userReportService.queryUserReport(-1, 1);
        List<UserReport>  reports = (List<UserReport>) result.getData().get("reports");

        Result result1 = userReportService.queryUserReport(2, 2);
        List<UserReport>  reports1 = (List<UserReport>) result1.getData().get("reports");
        reports.addAll(reports1);
//        result.data("reports",reports);
        return Result.ok().message("查找举报信息成功").data("reports",reports).count(reports.size());*/
        return userReportService.findAllReport();
    }
    @ResponseBody
    @RequestMapping(value = "/queryAllCommunityReport")
    @CrossOrigin
    public Result queryAllCommunityReport(Integer communityNo){
        return userReportService.queryAllCommunityReport(communityNo);
    }

}
